package dog.weatherwallpaper.model;

import java.text.DecimalFormat;
import java.util.Locale;

import org.json.JSONObject;

import android.content.Context;
import dog.weatherwallpaper.R;
import dog.weatherwallpaper.utils.App;
import dog.weatherwallpaper.utils.Strings;

public abstract class AbstractWeather {
	final char BR = '\n';
	final char SPACE = ' ';
	final char COLON = ':';
	final char PIPE = '|';

	double precipMM;
	int weatherCode = 113;
	String weatherDesc = "description";
	String weatherIconUrl = "wsymbol_0001_sunny.png";
	String winddir16Point = "N";
	int winddirDegree;
	int windspeedKmph;
	int windspeedMiles;

	public abstract String getBasicInfo(Unit unit);

	public abstract String getAllInfo(Unit unit);

	AbstractWeather(final JSONObject json) {
		if (json == null) return;
		this.precipMM = json.optDouble("precipMM");
		this.weatherCode = json.optInt("weatherCode");
		this.weatherDesc = Strings.getValueFromArray(json, "weatherDesc");
		this.weatherIconUrl = Strings.getValueFromArray(json, "weatherIconUrl");
		this.winddir16Point = json.optString("winddir16Point");
		this.winddirDegree = json.optInt("winddirDegree");
		this.windspeedKmph = json.optInt("windspeedKmph");
		this.windspeedMiles = json.optInt("windspeedMiles");
	}

	/**
	 * @return weatherDesc, if language is english, or string from descriptions.xml searched by weatherCode
	 */
	final String getWeatherDesc() {
		if (Locale.getDefault().getLanguage().equals(Locale.ENGLISH.getLanguage())) {
			return weatherDesc;
		} else {
			final int CLEAR_SUNNY_CODE = 113;
			final String SUNNY = "sunny";
			Context c = App.context();
			String letter = "c";
			if (weatherCode == CLEAR_SUNNY_CODE) {
				letter = weatherIconUrl.contains(SUNNY) ? "d" : "n";
			}
			int descID = c.getResources().getIdentifier(letter + weatherCode, "string", c.getPackageName());
			return c.getString(descID);
		}
	}

	/**
	 * Only shows decimals when needed
	 */
	final String getPrecipitation() {
		return new DecimalFormat("##.##").format(precipMM) + "mm";
	}

	final String getWindSpeed(boolean km) {
		StringBuilder sb = new StringBuilder();
		if (km) {
			sb.append(windspeedKmph);
			sb.append(App.context().getString(R.string.km));
		} else {
			sb.append(windspeedMiles);
			sb.append(App.context().getString(R.string.mi));
		}
		return sb.toString();
	}

	final String getWindDirection() {
		return SPACE + winddir16Point;
	}

	/**
	 * @return space+resourceString(first char)+colon+info
	 */
	final String createField(final int resourceID, final String info) {
		return "" + SPACE + App.context().getString(resourceID).charAt(0) + COLON + info;
	}

	/**
	 * @return resourceString+colon+space+info+break
	 */
	final String createLine(final int resourceID, final String info) {
		return App.context().getString(resourceID) + COLON + SPACE + info + BR;
	}

	public final String getIconName() {
		int start = weatherIconUrl.indexOf("wsymbol_00");
		int end = weatherIconUrl.lastIndexOf('.');
		return weatherIconUrl.substring(start, end);
	}
	
	public final double getPrecipMM() {
		return precipMM;
	}
}